import app from './boostrap/app';
import { cachePut, cacheGet } from './cache';
import { check } from './service/system';
const server = require('http').createServer(app.callback());
const io = require('socket.io')(server);

// socket连接
io.on('connection', (socket) => {
    socket.on('onDataChange', (msg) => {
        socket.broadcast.emit('onDataChange', msg);
    });
    socket.on('disconnect', () => {
        console.log('user disconnected');
    });

    socket.on("cachePut", async (msg)=> {
        console.log('cachePut happened!!', msg.key, msg.value);
        const val = await cacheGet(msg.key);
        if(val === msg.value){
            console.log("数值相同无须插入");
            
        }else{
            await cachePut(msg.key, msg.value);
        }
        
    })
});

// 监听端口
server.listen(8081, async () => {
    console.log('listening on *:8081');
    await cachePut("key", JSON.stringify({key: "123"}));
    const rlt = await cacheGet("key");
    console.log("=======================");
    
    console.log({rlt});
    console.log("=======================");

    
    if(await check()){
        const systemOwnerExist = await cacheGet("isSysOwnerExist");
        console.log(systemOwnerExist);
        
    }
});